<?php
namespace Controller;

use Teemo\MVC\Controller;
use Service\TestService;

/**
 * Created by PhpStorm.
 * User: lkk
 * Date: 2019/1/14
 * Time: 4:02 PM
 */
class Test extends Controller
{

    public function index()
    {

        return 'i am Test/index by Route';
    }

    /**
     * @return false|string
     * @throws \Exception
     * @desc 返回一个信息
     */
    public function getInfo()
    {
        $address_id = $this->request->getQueryParam('address_id');
        if (empty($address_id)) {
            throw new \Exception("address_id 不能为空 ");
        }
        $result = TestService::getInstance()->getInfoByPk($address_id);
        return json_encode($result);
    }

    /**
     * @return false|string
     * @desc 返回列表
     */
    public function getList()
    {
        $result = TestService::getInstance()->getList();
        return json_encode($result);
    }

    /**
     * @return bool
     * @desc 添加
     */
    public function add()
    {
        $array = [
            'title' => $this->request->getQueryParam('title'),
            'lng' => $this->request->getQueryParam('lng'),
            'lat' => $this->request->getQueryParam('lat'),
            'geohash' => $this->request->getQueryParam('geohash'),
            'address' => $this->request->getQueryParam('address'),
        ];

        return TestService::getInstance()->add($array);
    }

    /**
     * @return bool
     * @throws \Exception
     * @desc 更新信息
     */
    public function update()
    {
        $array      = ['address' => $this->request->get['address']];
        $address_id = $this->request->get['address_id'];
        return TestService::getInstance()->updateById($array, $address_id);
    }

    /**
     * @return mixed
     * @throws \Exception
     * @desc 删除信息
     */
    public function delete()
    {
        $address_id = $this->request->get['address_id'];
        return TestService::getInstance()->deleteById($address_id);
    }
}

